package com.wtf.dsp.admin.enums;

import com.wtf.dsp.core.enums.SqlType;

import java.util.Arrays;

/**
 * ClassName: SqlXml
 * Description: Mybatis的XML中SQL的标签格式化
 * date: 2023/7/18 20:35
 *
 * @author linhuanyou
 * @version 1.0
 */
public enum SqlMybatisXml {

    SELECT_XML(SqlType.SELECT,"  <select id=\"${sqlId}\">\n    ${sqlStatement}\n  </select>\n"),
    INSERT_XML(SqlType.INSERT,"  <insert id=\"${sqlId}\">\n    ${sqlStatement}\n  </insert>\n"),
    UPDATE_XML(SqlType.UPDATE,"  <update id=\"${sqlId}\">\n    ${sqlStatement}\n  </update>\n"),
    DELETE_XML(SqlType.DELETE,"  <delete id=\"${sqlId}\">\n    ${sqlStatement}\n  </delete>\n")
    ;

    private static final String SQL_ID = "${sqlId}";
    private static final String SQL_STATEMENT = "${sqlStatement}";

    private SqlType sqlType;

    private String sqlXml;

    SqlMybatisXml(SqlType sqlType, String sqlXml) {
        this.sqlType = sqlType;
        this.sqlXml = sqlXml;
    }

    public SqlType getSqlType() {
        return sqlType;
    }

    public void setSqlType(SqlType sqlType) {
        this.sqlType = sqlType;
    }

    public String getSqlXml() {
        return sqlXml;
    }

    public void setSqlXml(String sqlXml) {
        this.sqlXml = sqlXml;
    }

    public static String obtainSqlstatementXml(SqlType sqlType, String sqlId, String sqlStatement){
        SqlMybatisXml sqlMybatisXml = Arrays.stream(SqlMybatisXml.values()).filter(e -> {
            return e.getSqlType().equals(sqlType);
        }).findFirst().get();
        return sqlMybatisXml.getSqlXml().replace(SQL_ID,sqlId).replace(SQL_STATEMENT,sqlStatement);
    }
}
